package com.sinoglobal.ningxia.activity.shop;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.adapter.ShoppingListViewAdapter;
import com.sinoglobal.ningxia.adapter.ShoppingPagerAdapter;
import com.sinoglobal.ningxia.beans.MyCouponsActivityVo;
import com.sinoglobal.ningxia.beans.MyCouponsDataVo;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.dialog.DialogBuilder;
import com.sinoglobal.ningxia.dialog.MDialogMethod;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.AbstractActivity;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.thread.ItktAsyncTask;

/**
 * 
 * @author zbl
 * @createdate 2014-6-24 下午1:45:03
 * @Description:在精品商城中点击“我的”进入此界面
 */
public class MyCouponsActivity extends AbstractActivity implements OnPageChangeListener, OnClickListener, OnItemLongClickListener,
		OnItemClickListener {
	private LinearLayout secBar;// 现金购买or积分兑换布局
	private TextView[] secTexts;// 现金购买or积分兑换
	private ViewPager goodsViewPager;// 展示活动
	private List<View> list = new ArrayList<View>();
	private View cashView, scoreView;// listview里面的子布局
	private ShoppingPagerAdapter adapter;
	private ListView cashListView, scoreListView;
	private ShoppingListViewAdapter cashListViewAdapter, scoreListViewAdapter;// listview的adapter
	private ArrayList<MyCouponsDataVo> CashDataVo;// 我的里面的数据集
	private ArrayList<MyCouponsDataVo> ScoreDataVo;// 我的里面的数据集
	// private int type;// 1现金 2积分
	private final String CASH = "1";
	private final String SCORE = "2";
	private final String BOG_ID_STRING = "bog_id";
	private String bogId;// 订单号
	private String type;// 1.实物 2.活动
	private String goodsId;// 商品，活动ID
	private RelativeLayout cashGoods, scoreGoods;
	private TextView phoneNum;// 客服电话
	private RelativeLayout customerService;// 客服

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isTemplate = true;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mallshop_mycoupons);
		titleView.setText(R.string.mycoupons);
		init();// 初始化组件
		// 从网络获取数据
		loadInfo(CASH, true);
		loadInfo(SCORE, false);
	}

	public void init() {
		goodsViewPager = (ViewPager) findViewById(R.id.goods_vpager);
		secBar = (LinearLayout) findViewById(R.id.sec_bar);
		phoneNum = (TextView) findViewById(R.id.phonenum);
		customerService = (RelativeLayout) findViewById(R.id.kefu);

		cashView = LayoutInflater.from(this).inflate(R.layout.mallshop_viewpager_listview, null);
		list.add(cashView);

		scoreView = LayoutInflater.from(this).inflate(R.layout.mallshop_viewpager_listview, null);
		list.add(scoreView);
		adapter = new ShoppingPagerAdapter(list);
		goodsViewPager.setAdapter(adapter);
		goodsViewPager.setOnPageChangeListener(this);

		secTexts = new TextView[secBar.getChildCount()];
		for (int i = 0; i < secTexts.length; i++) {
			secTexts[i] = (TextView) secBar.getChildAt(i);
			secTexts[i].setOnClickListener(this);
		}
		secTexts[0].setEnabled(false);
		goodsViewPager.setCurrentItem(0);// 让viewpager显示在第一个页面并激活第一个标题
		cashListView = (ListView) cashView.findViewById(R.id.mycoupons_listView);
		scoreListView = (ListView) scoreView.findViewById(R.id.mycoupons_listView);
		cashListView.setOnItemLongClickListener(this);
		scoreListView.setOnItemLongClickListener(this);
		cashListView.setOnItemClickListener(this);
		scoreListView.setOnItemClickListener(this);

		cashGoods = (RelativeLayout) cashView.findViewById(R.id.nogoods);
		scoreGoods = (RelativeLayout) scoreView.findViewById(R.id.nogoods);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.my_coupons, menu);
		return true;
	}

	@Override
	public void onClick(View v) {

		// 判断是否是当前点击的view
		for (int i = 0; i < secTexts.length; i++) {
			if (v == secTexts[i]) {
				for (TextView t : secTexts) {
					t.setEnabled(true);
				}
				secTexts[i].setEnabled(false);
				goodsViewPager.setCurrentItem(i);

			}
		}
	}

	@Override
	public void onPageSelected(int arg0) {
		for (int i = 0; i < secTexts.length; i++) {
			if (secTexts[arg0] == secTexts[i]) {
				for (TextView t : secTexts) {
					t.setEnabled(true);
				}
				secTexts[i].setEnabled(false);
			}
		}

	}

	// 长按删除已有的活动/物品
	@Override
	public boolean onItemLongClick(AdapterView<?> parentview, View view, final int position, long id) {

		if (parentview == cashListView) {
			final DialogBuilder dialogBuilder = new DialogBuilder(MyCouponsActivity.this);
			dialogBuilder.Dialogbtn2(getString(R.string.sure_delete_activity), R.layout.dialog_two_button, new MDialogMethod() {
				@Override
				public void dialogOk() {
					// 删除选中的item
					CashDataVo.remove(position);
					cashListViewAdapter.notifyDataSetChanged();
					// 请求服务器告诉后台清空列表

					// 请求服务器告诉后台清空列表805
					new ItktAsyncTask<Void, Void, RootVo>() {

						@Override
						public RootVo before(Void... params) throws Exception {
							// 第二个参数id传“”的时候代表清空所有
							return RemoteImpl.getInstance().deleteCashGoodActivity(CashDataVo.get(position).getLog_id());
						}

						@Override
						public void after(RootVo result) {
							if (result != null) {
								if (Code.CODE_SUCCESS != result.getCode()) {
									showShortToastMessage(result.getMessage());
									// 清空之后要消失改dialog
									dialogBuilder.mdialog.dismiss();
								} else {
									showShortToastMessage(result.getMessage());
									// 清空之后要消失改dialog
									dialogBuilder.mdialog.dismiss();

								}
							}

						}

						@Override
						public void exception() {

						}
					}.execute();

					// 清空之后要消失改dialog
					dialogBuilder.mdialog.dismiss();
				}

				@Override
				public void dialogCancel() {
					dialogBuilder.mdialog.dismiss();
				}
			});

		} else if (parentview == scoreListView) {
			final DialogBuilder dialogBuilder = new DialogBuilder(MyCouponsActivity.this);
			dialogBuilder.Dialogbtn2(getString(R.string.sure_delete_activity), R.layout.dialog_two_button, new MDialogMethod() {
				@Override
				public void dialogOk() {
					// 请求服务器告诉后台清空列表
					new ItktAsyncTask<Void, Void, RootVo>() {

						@Override
						public RootVo before(Void... params) throws Exception {
							// 第二个参数id传“”的时候代表清空所有
							return RemoteImpl.getInstance().deleteCashGoodActivity(ScoreDataVo.get(position).getLog_id());
						}

						@Override
						public void after(RootVo result) {
							if (result != null) {
								if (Code.CODE_SUCCESS != result.getCode()) {
									showShortToastMessage(result.getMessage());
									// 清空之后要消失改dialog
									dialogBuilder.mdialog.dismiss();
								} else {
									// 删除选中的item
									ScoreDataVo.remove(position);
									scoreListViewAdapter.notifyDataSetChanged();
									showShortToastMessage(result.getMessage());
									// 清空之后要消失改dialog
									dialogBuilder.mdialog.dismiss();

								}
							}

						}

						@Override
						public void exception() {

						}
					}.execute();
					// 清空之后要消失改dialog
					dialogBuilder.mdialog.dismiss();
				}

				@Override
				public void dialogCancel() {
					dialogBuilder.mdialog.dismiss();
				}
			});

		}

		return false;
	}

	/**
	 * 
	 * @author zbl
	 * @createdate 2014-6-26 上午11:02:24
	 * @Description: (803从服务器上加载现金购买的信息数据)
	 * 
	 */
	private void loadInfo(final String type, boolean bolg) {
		new ItktAsyncTaskWithDialog<Void, Void, MyCouponsActivityVo>(getString(R.string.loading), bolg, true) {

			@Override
			public MyCouponsActivityVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getMyCouponsActivityVo(type);
			}

			@Override
			public void after(MyCouponsActivityVo result) {
				if (result != null) {
					if (Code.CODE_SUCCESS == result.getCode()) {
						customerService.setVisibility(View.VISIBLE);// 客服电话显示
						phoneNum.setText(MessageFormat.format(getString(R.string.service_tel), result.getNumber()));
						if (CASH.equals(type)) {//现金购买
							// 从服务器获得的数据通过Adapter显示到列表上
							cashGoods.setVisibility(View.GONE);
							CashDataVo = result.getList();
							cashListViewAdapter = new ShoppingListViewAdapter(MyCouponsActivity.this, CashDataVo);
							cashListView.setAdapter(cashListViewAdapter);
						} else if (SCORE.equals(type)) {//积分兑换
							scoreGoods.setVisibility(View.GONE);
							ScoreDataVo = result.getList();
							scoreListViewAdapter = new ShoppingListViewAdapter(MyCouponsActivity.this, ScoreDataVo);
							scoreListView.setAdapter(scoreListViewAdapter);
						}

					} else if (2 == result.getCode()) {
						customerService.setVisibility(View.GONE);// 客服电话隐藏
						if (CASH.equals(type)) {
							cashListView.setVisibility(View.GONE);
							cashGoods.setVisibility(View.VISIBLE);
						} else if (SCORE.equals(type)) {
							scoreGoods.setVisibility(View.VISIBLE);// 没有商品图标显示
							scoreListView.setVisibility(View.GONE);// 列表隐藏

						}

					} else {
						showShortToastMessage(result.getMessage());
					}
				}

			}

			@Override
			public void exception() {

			}
		}.execute();

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	// 点击进入查看此物品的详情
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// 若为现金购买
		if (parent == cashListView) {
			type = CashDataVo.get(position).getType2();
			bogId = CashDataVo.get(position).getLog_id();// 获取订单号
			goodsId = CashDataVo.get(position).getGoods_id();
			// 1.实物则跳转到物流界面，进行物流的查看
			if (type.equals(CASH)) {
				Intent intent = new Intent(this, CashGoodsInfoActivity.class);
				intent.putExtra(BOG_ID_STRING, bogId);
				startActivity(intent);
			} else if (type.equals(SCORE)) {
				// 现金购买的活动则跳转到活动的详情页
				// 将
				Intent intent = new Intent(this, CashPurchasedActivity.class);
				intent.putExtra(BOG_ID_STRING, bogId);
				intent.putExtra("goods_id", goodsId);
				startActivity(intent);
			}

		}
		// 若为积分兑换
		if (parent == scoreListView) {
			type = ScoreDataVo.get(position).getType2();
			bogId = ScoreDataVo.get(position).getLog_id();
			goodsId = ScoreDataVo.get(position).getGoods_id();
			// 1.实物则跳转到物流界面，进行物流的查看
			if (type.equals(CASH)) {
				Intent intent = new Intent(this, CashGoodsInfoActivity.class);
				intent.putExtra(BOG_ID_STRING, bogId);
				startActivity(intent);
			} else if (type.equals(SCORE)) {
				// 积分兑换的活动则跳转到活动的详情页
				Intent intent = new Intent(this, ScorePurchasedActivity.class);
				intent.putExtra(BOG_ID_STRING, bogId);
				intent.putExtra("goods_id", goodsId);
				startActivity(intent);
			}

		}
	}

}
